Method 2 : Using columns[]
This method will return the column based on index. So, we have to give 0 to get the first column
Syntax:
dataframe[dataframe.columns[0]]
where
- dataframe is the input dataframe
- columns[0] represent first column
Example: Python program to get the first column using columns[]
Python3
# import pandas module import pandas as pd # create dataframe with 3 columns data = pd.DataFrame({ "id" : [ 7058 , 7059 , 7072 , 7054 ], "name" : [ 'sravan' , 'jyothika' , 'harsha' , 'ramya' ], "subjects" : [ 'java' , 'python' , 'html/php' , 'php/js' ] } ) # display dataframe print (data) print ( "--------------" ) # get first column by returning dataframe # using columns[] method print (data[data.columns[ 0 ]]) |
Output:
How to Get First Column of Pandas DataFrame?
In this article, we will discuss how to get the first column of the pandas dataframe in Python programming language.